package com.upc.cisco.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.upc.cisco.dominio.Alumno;
import com.upc.cisco.excepcion.DAOExcepcion;
import com.upc.cisco.util.ConexionBD;

public class AlumnoDao extends BaseDao {

	public void registrar(Alumno alumno) throws DAOExcepcion {
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con
					.prepareStatement("INSERT INTO alumno(nombres,paterno,materno,sexo,tipodoc,numerodoc,celular) VALUES(?,?,?,?,?,?,?)");
			stmt.setString(1, alumno.getNombre());
			stmt.setString(2, alumno.getPaterno());
			stmt.setString(3, alumno.getMaterno());
			stmt.setString(4, alumno.getSexo());
			stmt.setString(5, alumno.getTipoDoc());
			stmt.setString(6, alumno.getNumeroDoc());
			stmt.setString(7, alumno.getCelular());
			stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public void actualizar(Alumno alumno) throws DAOExcepcion {
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con
					.prepareStatement("UPDATE alumno SET nombres=?,paterno=?,materno=?,sexo=?,tipodoc=?,numerodoc=?,celular=? WHERE idalumno=?");
			stmt.setString(1, alumno.getNombre());
			stmt.setString(2, alumno.getPaterno());
			stmt.setString(3, alumno.getMaterno());
			stmt.setString(4, alumno.getSexo());
			stmt.setString(5, alumno.getTipoDoc());
			stmt.setString(6, alumno.getNumeroDoc());
			stmt.setString(7, alumno.getCelular());
			stmt.setInt(8, alumno.getIdAlumno());
			stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public void eliminar(Alumno alumno) throws DAOExcepcion {
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con
					.prepareStatement("DELETE FROM alumno WHERE idalumno = ?");
			stmt.setInt(1, alumno.getIdAlumno());
			stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public boolean existDNI(String dni) throws DAOExcepcion {
		boolean exist = false;
		String query = "select * from alumno where numerodoc=?";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, dni);
			rs = stmt.executeQuery();
			if (rs.next()) {
				exist = true;
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return exist;
	}
	
	public List<Alumno> listar() throws DAOExcepcion {
		String query = "select * from alumno";
		List<Alumno> lista = new ArrayList<Alumno>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Alumno alu = new Alumno();
				alu.setIdAlumno(rs.getInt(1));
				alu.setNombre(rs.getString(2));
				alu.setPaterno(rs.getString(3));
				alu.setMaterno(rs.getString(4));
				alu.setSexo(rs.getString(5));
				alu.setTipoDoc(rs.getString(6));
				alu.setNumeroDoc(rs.getString(7));
				alu.setCelular(rs.getString(8));
				lista.add(alu);
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}
}
